﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using HYView;

namespace YJKArch.DoorWinModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdLeftRightTurn : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)3;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Autodesk.Revit.DB.Document document = cmdData.Application.ActiveUIDocument.Document;
			Result result;
			for (;;)
			{
				try
				{
					using (Transaction transaction = new Transaction(this.m_doc, "temp"))
					{
						transaction.Start();
						Selection selection = cmdData.Application.ActiveUIDocument.Selection;
						ISelectionFilter selectionFilter = new DoorWinFilter();
						foreach (Element element in selection.PickElementsByRectangle(selectionFilter, "请框选要左右翻转的门窗"))
						{
							FamilyInstance familyInstance = RevitVersionFuncs.GetElement(this.m_doc, element.Id) as FamilyInstance;
							if (familyInstance.CanFlipHand)
							{
								familyInstance.flipHand();
							}
						}
						transaction.Commit();
					}
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					result = base.YJKExcute(cmdData, ref message, elements);
				}
				catch (Exception ex)
				{
					string message2 = ex.Message;
					result = base.YJKExcute(cmdData, ref message, elements);
				}
				break;
			}
			return result;
		}
	}
}
